﻿using System;
using UnityEngine;
namespace FrameWorkSong
{
    public static class CameraExtention
    {
        [Obsolete("方法已过时，请使用GetCameraMovePos(Bounds target)")]
        public static Vector3 GetCameraMovePos(this Camera camera, GameObject target)
        {
            Vector3 length = target.GetComponent<MeshFilter>().mesh.bounds.size;
            Vector3 scale = target.transform.lossyScale;
            float size = length.magnitude * scale.magnitude;
            Vector3 vector = Vector3.zero;
            vector = GetCenter(target.transform) - (camera.transform.forward * size);
            return vector;
        }
        static Vector3 GetCenter(Transform target)
        {
            Mesh mesh = target.GetComponent<MeshFilter>().mesh;
            Vector3[] vector3s = mesh.vertices;
            Vector3 all = Vector3.zero;
            int length = vector3s.Length;
            for (int i = 0; i < length; i++)
            {
                Debug.Log(vector3s[i]);
                all += vector3s[i] + target.position;
            }
            return all / length;
        }
        /// <summary>
        /// 得到Camera定位目标移动位置
        /// </summary>
        /// <param name="camera"></param>
        /// <param name="target">目标的Bound</param>
        /// <returns></returns>
        public static Vector3 GetCameraMovePos(this Camera camera, Bounds target)
        {
            Vector3 vector = Vector3.zero;
            vector = target.center - (camera.transform.forward * target.size.magnitude);
            return vector;
        }
        /// <summary>
        /// 得到Camera定位目标移动位置
        /// </summary>
        /// <param name="camera"></param>
        /// <param name="target">目标的位置</param>
        /// <returns></returns>
        public static Vector3 GetCameraMovePos(this Camera camera, Vector3 target)
        {
            Vector3 vector = Vector3.zero;
            Vector3 targetVec =  target - camera.transform.position ;
            float projectionDis = targetVec.GetProjectionMagnitude(camera.transform.forward);
            vector = target - (camera.transform.forward * projectionDis);
            return vector;
        }
    }
}
